package main

import (
	"net"
	"strings"
	"fmt"
)

type User struct {
	Name string
	Addr string
	C chan string
	conn net.Conn

	server *Server
}

func NewUser(conn net.Conn, server *Server) *User {
	userAddr := conn.RemoteAddr().String()

	user := &User {
		Name: userAddr,
		Addr : userAddr,
		C : make(chan string),
		conn : conn,
		server: server,
	}

	go user.ListenMessage()

	return user
}


func (this *User) ListenMessage() {
	for {
		msg := <- this.C
		this.conn.Write([]byte(msg + "\n"))
	}
}

///*
func (this *User) Online() {
	this.server.mapLock.Lock()		
	this.server.OnlineMap[this.Name] = this
	this.server.mapLock.Unlock()		

	this.server.BroadCast(this, "已上线")
	// this.DoMessage("已上线")
}

func (this *User) Offline() {
	this.server.mapLock.Lock()		
	delete(this.server.OnlineMap, this.Name)
	this.server.mapLock.Unlock()		

	this.server.BroadCast(this, "已下线")

	// this.DoMessage("已下线")
}


func (this *User) SendMsg(msg string) {
	this.conn.Write([]byte(msg))
}

func (this *User) DoMessage(msg string) {
	if msg[0:3] == "who" {
		//查询当前在线用户都有哪些
		this.server.mapLock.Lock()		
		for _, user := range this.server.OnlineMap {
			onlineMsg := "[ " + user.Addr + " ] " + user.Name + " : " + "在线...\n"
			this.SendMsg(onlineMsg)
			// this.C <- onlineMsg
		}
		this.server.mapLock.Unlock()		

	} else if len(msg) > 7 && msg[:7] == "rename|"{
		newName := strings.Split(msg, "|")[1]

		// 去掉最后一个 \n 或者是 \r 的字符
		newName = newName[:len(newName)-1]
		_, ok := this.server.OnlineMap[newName]
		if ok {
			this.SendMsg("当前用户名被使用\n")
		} else {
			this.server.mapLock.Lock()
			delete(this.server.OnlineMap, this.Name)
			this.server.OnlineMap[newName] = this
			this.server.mapLock.Unlock()

			this.Name = newName
			fmt.Println("newName: ", newName, " len = ", len(newName))	
			this.SendMsg("您已经更新用户名：" + this.Name + "\n")

		}
	} else {
		this.server.BroadCast(this, msg)
	}
}
// */
